import hashlib
from ..serializers.admin import *


# 校验密码
def auth(admin, password):
    # 第一次MD5加密
    first_md5 = hashlib.md5(password.encode('utf-8')).hexdigest()
    # 拼接盐值并进行第二次MD5加密
    second_md5 = hashlib.md5((first_md5 + admin.salt).encode('utf-8')).hexdigest()

    if admin.password == second_md5:
        return True
    return False


# 登录接口
def loginAPI(username, password):
    admin = FaAdmin.objects.filter(username=username).first()
    if admin:
        if auth(admin, password):
            group_access = FaAuthGroupAccess.objects.filter(uid=admin.id).first()
            group = FaAuthGroup.objects.get(id=group_access.group_id)
            return group.name, admin.id
    return False, False
